package com.wz.project.pda.controller;

import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.api.barcode.domain.Barcode;
import com.wz.project.api.barcode.domain.BarcodeInfo;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.barcode.service.BarcodeService;
import com.wz.project.api.sap.controller.SapController;
import com.wz.project.api.tmsMatnr.domain.TmMatnr;
import com.wz.project.api.tmsMatnr.service.ITmMatnrService;
import com.wz.project.barcodeMsg.domain.BarcodeMsg;
import com.wz.project.barcodeMsg.service.IBarcodeMsgService;
import com.wz.project.bas.product.domain.Product;
import com.wz.project.bas.product.service.IProductService;
import com.wz.project.inStock.stockindetail.domain.InStockinDetail;
import com.wz.project.mydo.domain.MydoJobDetail;
import com.wz.project.mydo.mydojob.service.IMydoJobService;
import com.wz.project.mydo.mydojobdetail.service.IJobDetailService;
import com.wz.project.outStock.stockout.domain.OutStockout;
import com.wz.project.outStock.stockout.service.IOutStockoutService;
import com.wz.project.outStock.stockoutdetail.domain.OutStockoutDetail;
import com.wz.project.outStock.stockoutdetail.service.IOutStockoutDetailService;
import com.wz.project.outTray.outTray.domain.OutTrayMsg;
import com.wz.project.outTray.outTray.service.IOutTrayMsgService;
import com.wz.project.pda.service.PdaService;
import com.wz.project.sap.DN.domain.ZflWmsGetDn;
import com.wz.project.sap.DN.service.IZflWmsGetDnService;
import com.wz.project.sap.DN_POST_DN.domain.ZflWmsDnPostDn;
import com.wz.project.sap.DN_POST_DN.service.IZflWmsDnPostDnService;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.stockdetail.domain.BasStockDetail;
import com.wz.project.stock.stockdetail.service.IBasStockDetailService;
import com.wz.project.surebarcode.sureBarcode.domain.Surebarcode;
import com.wz.project.surebarcode.sureBarcode.service.ISurebarcodeService;
import com.wz.project.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pdaPk")
public class PdaPController1 extends BaseController {

    private String prefix = "rf";

    @Autowired
    private IOutTrayMsgService outTrayMsgService;
    @Autowired
    private ITmMatnrService tmMatnrService;
    @Autowired
    private IOutStockoutDetailService outStockoutDetailService;

    @Autowired
    private IOutStockoutService outStockoutService;
    @Autowired
    PdaService pdaService;
    @Autowired
    SapController sapController;
    @Autowired
    ITmMatnrService matnrService;
    @Autowired
    IMydoJobService mydoJobService;
    @Autowired
    IJobDetailService mydoJobDetailService;
    @Autowired
    IZflWmsGetDnService ZflWmsGetDnService;
    @Autowired
    IProductService productService;
    @Autowired
    IZflWmsDnPostDnService zflWmsDnPostDnService;
    @Autowired
    IBarcodeMsgService barcodeMsgService;
    @Autowired
    BarcodeService barcodeService;
    @Autowired
    IBasStockDetailService basStockDetailService;

    @Autowired
    ISurebarcodeService surebarcodeService;

    @GetMapping("/productsh")
    public String productsh() {
        return prefix + "/productsh";
    }

    @GetMapping("/productInP")
    public String productInP() {
        return prefix + "/productInP";
    }

    @GetMapping("/barcodeget")
    public String barcodeget() {
        return prefix + "/barcodeget";
    }

    @GetMapping("/recover")
    public String recover() {
        return prefix + "/recover";
    }

    @GetMapping("/productzj")
    public String DN_POST_DN() {
        return prefix + "/productzj";
    }

    @RequestMapping("/jobTpAdd")
    @ResponseBody
    @Log(title = "空托盘入库", businessType = BusinessType.INSERT)
    public AjaxResult jobTpAdd( @RequestBody BasStock basStock) {
        AjaxResult ajaxResult = pdaService.jobTpAdd(basStock.getTrayCode(), basStock.getQuantity().intValue());
        return ajaxResult;

    }
    @RequestMapping("/jobTpOut")
    @ResponseBody
    @Log(title = "空托盘出库", businessType = BusinessType.INSERT)
    public AjaxResult jobTpOut( @RequestBody BasStock basStock) {
        AjaxResult ajaxResult = pdaService.jobTpOut(basStock.getLineId(), basStock.getQuantity().intValue());
        return ajaxResult;

    }
    /**
     * 确认收货
     * @param basStock
     * @return
     */
    @RequestMapping("/barcodesave")
    @ResponseBody
    @Log(title = "条码采集", businessType = BusinessType.INSERT)
    public AjaxResult barcodesave(@RequestBody BasStock basStock) {
        AjaxResult ajaxResult = new AjaxResult();
        String fbarcode = basStock.getBarcode();
        String trayCode = basStock.getTrayCode();
        String cargoSpaceId = basStock.getCargoSpaceId();
        BarcodeMsg barcodeMsg1 = barcodeMsgService.selectBarcodeMsgById(fbarcode);
        if (barcodeMsg1 == null) {
            BarcodeMsg barcodeMsg = new BarcodeMsg();
            barcodeMsg.setTrayCode(trayCode);
            barcodeMsg.setBarcode(fbarcode);
            barcodeMsg.setCargoSpaceId(cargoSpaceId);
            barcodeMsg.setCreateTime(new Date());
            barcodeMsgService.insertBarcodeMsg(barcodeMsg);
            ajaxResult.put("res", "数据采集成功");
        } else {
            ajaxResult.put("res", "条码数据重复");
        }
        return ajaxResult;

    }
    @RequestMapping("/stockSave")
    @ResponseBody
    @Log(title = "扫描新增库存", businessType = BusinessType.INSERT)
    public AjaxResult stockSave( @RequestBody BasStock basStock) {
        AjaxResult ajaxResult = pdaService.addStock(basStock.getTrayCode(), basStock.getCargoSpaceId(), basStock.getBarcode());
        return ajaxResult;

    }
    /**
     * 收货确认数据获取
     * @param tmMatn
     * @return
     */
    @RequestMapping("/tms/listSH")
    @ResponseBody
    @Log(title = "条码扫描收货", businessType = BusinessType.INSERT)
    public Map selectTmsMsgSH(@RequestBody  TmMatnr tmMatn ) {
        String fbarcode=tmMatn.getFbarcode();
        String stockInId = tmMatn.getInStockId();

        Map map = new HashMap<>();
        //查询是否同步交货单号
        ZflWmsGetDn zflWmsGetDn = ZflWmsGetDnService.selectZflWmsGetDnById1(String.format("%010d", Long.parseLong(stockInId)));
        if (zflWmsGetDn == null) {
            sapController.getDNData(String.format("%010d", Long.parseLong(stockInId)), "", "");
        }
        //查询条码是否已入库
        BasStockDetail basStockDetail = basStockDetailService.selectBasStockDetailById(fbarcode);
        if (basStockDetail != null) {
            map.put("isnull", "该条码已入库，无需重复扫描");
            return map;
        }
        //查询计算该单据所有子件总数
        InStockinDetail inStockinDetail1 = pdaService.selectInstock(String.format("%010d", Long.parseLong(stockInId)));
        if (inStockinDetail1 != null) {
            map.put("qty", inStockinDetail1.getQuantity());
        }
        //查询条码是否已扫描未入库
        TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
        if (tmMatnr1 != null) {
            //按照单据，物料子件分组统计各物料扫描数量
            List<TmMatnr> tmMatnr = matnrService.selectTmMatnrByInstock(String.format("%010d", Long.parseLong(stockInId)));
            if (tmMatnr.size() > 0) {
                map.put("matnrList", tmMatnr);
            }
            //查询该单据已扫描总数
            List<TmMatnr> tmMatnrl = matnrService.selectTmMatnrByStickInIde(String.format("%010d", Long.parseLong(stockInId)));
            map.put("qtyout", tmMatnrl.size());
            map.put("isnull", "该条码已扫描，无需重复扫描");
            return map;
        }
        //查询物料子母件信息
        Barcode barcode = null;
        try {
            Barcode barcode1 = pdaService.selectBarcodeBd(fbarcode);
            if (barcode1 == null) {
                barcode = barcodeService.selectBarcodeByIdHs(fbarcode);
                if (barcode != null) {
                    pdaService.saveBarcode(barcode);
                }
            } else {
                barcode = barcode1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isnull", "条码平台异常！！！");
            return map;
        }
        String stockinGuid = "";
        if (barcode != null) {

            //生产订单号（成品）在v_tm_matnr_minfo只有一条信息
            if (barcode.getAufnr() != null && barcode.getAufnr() != "") {
                stockinGuid = barcode.getAufnr();
                map.put("isnull", "该条码非配套物资无需收货");
                return map;
            } else {
                //采购订单号（配套）在v_tm_matnr_minfo有多条信息
                stockinGuid = String.format("%010d", Long.parseLong(stockInId));
            }
            //用物料和单据号查询子单据信息，如果有此物料的单据
            String barcodes = String.format("%018d", Long.parseLong(barcode.getMatnr().split("-")[0]));

            List<InStockinDetail> inStockinDetails = pdaService.selectInstockDetail(stockinGuid, barcode.getMatnr());

            if (inStockinDetails.size() >0) {
                InStockinDetail inStockinDetail = inStockinDetails.get(0);
                List<MatnrInfo> matnrInfoList = null;

                List<MatnrInfo> matnrInfoLists = pdaService.selectMatnrInfobd(barcode.getMatnr().split("-")[0]);
                if (matnrInfoLists.size() == 0) {
                    //从v_tm_matnr_minfo表中用母件码查询所有齐套件信息
                    matnrInfoList = pdaService.selectMatnrInfo(barcode.getMatnr().split("-")[0]);
                    if (matnrInfoList.size() > 0) {
                        for (MatnrInfo m : matnrInfoList) {
                            pdaService.insetMatnrInfo(m);
                        }
                    }else{
                        map.put("isnull", "此物料信息子母件条码平台未维护");
                        return map;
                    }
                } else {
                    matnrInfoList = matnrInfoLists;
                }
                MatnrInfo matnrInfo = pdaService.selectMatnrBdById(barcode.getMatnr());
                TmMatnr tmMatnr = new TmMatnr();
                tmMatnr.setMatnr(matnrInfo.getMatnr());
                tmMatnr.setMaktx(matnrInfo.getMaktx());
                tmMatnr.setMatnrMain(barcodes);
                tmMatnr.setYkutxt(matnrInfo.getYkutxt());
                tmMatnr.setEwbez(matnrInfo.getEwbez());
                tmMatnr.setFunit(matnrInfo.getFunit());
                tmMatnr.setFpackunit(matnrInfo.getFpackunit());
                tmMatnr.setFflag(matnrInfo.getFflag());
                tmMatnr.setFnum(matnrInfo.getFnum());
                tmMatnr.setFbarcode(barcode.getFbarcode());
                tmMatnr.setStatus(-1);
                tmMatnr.setLotNo(new SimpleDateFormat("yyyyMMdd").format(new Date()));
                tmMatnr.setInStockId(inStockinDetail.getStockinGuid());
                tmMatnr.setInStockDId(inStockinDetail.getPrintDate());
                matnrService.insertTmMatnr(tmMatnr);

                map.put("isnull", "该物料子件扫描成功");
                pdaService.updateInstockDetail(stockinGuid, matnrInfo.getMatnr(),inStockinDetail.getStockinDetailId());
                List<TmMatnr> tmMatnrl = matnrService.selectTmMatnrByStickInIde(String.format("%010d", Long.parseLong(stockInId)));
                List<TmMatnr> tmMatnrls = matnrService.selectTmMatnrByInstock(String.format("%010d", Long.parseLong(stockInId)));

                map.put("matnrList", tmMatnrls);
                map.put("qtyout", tmMatnrl.size());
            }else {
                List<TmMatnr> tmMatnr = matnrService.selectTmMatnrByInstock(String.format("%010d", Long.parseLong(stockInId)));
                if (tmMatnr.size() > 0) {
                    map.put("matnrList", tmMatnr);
                    map.put("isnull", "扫描子件已达上限");
                }else{
                    map.put("isnull", "无对应的收货单");
                }
                List<TmMatnr> tmMatnrl = matnrService.selectTmMatnrByStickInIde(String.format("%010d", Long.parseLong(stockInId)));

                map.put("qtyout", tmMatnrl.size());


                return map;
            }
        } else {
            map.put("isnull", "此条码条码平台未进行维护");
        }
        return map;
    }

    /**
     * 确认收货
     * @param tmMatn
     * @return
     */
    @RequestMapping("/SureSH")
    @ResponseBody
    @Log(title = "收货确认", businessType = BusinessType.UPDATE)
    public AjaxResult SureSH( @RequestBody  TmMatnr tmMatn ) {
        String stockInId = tmMatn.getInStockId();
        AjaxResult ajaxResult = new AjaxResult();
        stockInId = String.format("%010d", Long.parseLong(stockInId));
        InStockinDetail inStockinDetail1 = pdaService.selectInstock(stockInId);
        int fnum = 0;
        if (inStockinDetail1 != null) {
            fnum = inStockinDetail1.getQuantity().intValue();
        }
        List<TmMatnr> tmMatnrs = matnrService.selectTmMatnrByStickInIde(stockInId);
        int saomiaosize = matnrService.selectTmMatnrGroupProduct(stockInId);
        int instockincount = pdaService.selectCount(stockInId);
       if (tmMatnrs.size() < fnum || saomiaosize <instockincount) {
            ajaxResult.put("status", "该条码未齐套，无法收货");
            return ajaxResult;
        }

        for (TmMatnr tmMatnr : tmMatnrs) {
            if (tmMatnr != null && tmMatnr.getStatus() == -1 && "1".equals(tmMatnr.getFflag())) {
                String productLotNo = "";//是否批次管理
                String productXlh = "";//序列号
                Product product = productService.selectProductByProductId(String.format("%018d", Long.parseLong(tmMatnr.getMatnr().split("-")[0])));
                if (product != null) {
                    productLotNo = product.getSpecialFlag();
                    productXlh = product.getProductFlag();
                }
                ZflWmsGetDn zflWmsGetDn = new ZflWmsGetDn();
                zflWmsGetDn.setMATNR(StringUtil.Stringadd(tmMatnr.getMatnr().split("-")[0]));
                zflWmsGetDn.setVBELN(tmMatnr.getInStockId());
                zflWmsGetDn.setPOSNR(tmMatnr.getInStockDId());
                List<ZflWmsGetDn> zflWmsGetDns = ZflWmsGetDnService.selectZflWmsGetDnList(zflWmsGetDn);
                for (ZflWmsGetDn zflWmsGetDna : zflWmsGetDns) {
                    String lotNo = "";
                    ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                    if ("X".equals(productLotNo)) {
                        zflWmsDnPostDn.setCHARG(tmMatnr.getLotNo());
//                        zflWmsDnPostDn.setCHARG(new SimpleDateFormat("yyyyMMdd").format(new Date()));
                    }
                    if ("X".equals(productXlh)) {
                        zflWmsDnPostDn.setSERNR(tmMatnr.getFbarcode().substring(4, tmMatnr.getFbarcode().length()));
                        zflWmsDnPostDn.setLFIMG(tmMatnr.getFnum().toString());
                    } else {
                        zflWmsDnPostDn.setLFIMG(zflWmsGetDna.getLFIMG());
                    }

                    zflWmsDnPostDn.setZEILE(tmMatnr.getInStockDId());
                    zflWmsDnPostDn.setVBELN(zflWmsGetDna.getVBELN());
                    zflWmsDnPostDn.setPOSNR(tmMatnr.getInStockDId());
                    zflWmsDnPostDn.setMATNR(zflWmsGetDna.getMATNR());
                    zflWmsDnPostDn.setPIKMG("0");
                    zflWmsDnPostDn.setWERKS(zflWmsGetDna.getWERKS());
                    zflWmsDnPostDn.setLGORT(zflWmsGetDna.getLGORT());
                    zflWmsDnPostDn.setVRKME(zflWmsGetDna.getVRKME());

                    int size = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn).size();
                    zflWmsDnPostDn.setStatus("-1");
                    if (size == 0) {
                        zflWmsDnPostDnService.insertZflWmsDnPostDn(zflWmsDnPostDn);
                    }
                }

                ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                zflWmsDnPostDn.setVBELN(stockInId);
                zflWmsDnPostDn.setStatus("-1");
                int size = zflWmsDnPostDnService.selectZflWmsDnPostDnCount(zflWmsDnPostDn);
                int i = ZflWmsGetDnService.selectZflWmsGetDn(String.format("%010d", Long.parseLong(stockInId)));
                if (size == i) {
                    zflWmsDnPostDn.setStatus("0");
                    zflWmsDnPostDnService.updateZflWmsDnPostDnStatus(zflWmsDnPostDn);
                    pdaService.updatemMatnrs(stockInId);
                }
                ajaxResult.put("status", "收货成功");
            } else if (tmMatnr != null && tmMatnr.getStatus() == 0) {
                ajaxResult.put("status", "已收货");
            }
        }
        return ajaxResult;

    }
    @RequestMapping("/list1")
    @ResponseBody
    @Log(title = "质检查询", businessType = BusinessType.OTHER)
    public TableDataInfo list1(@RequestBody ZflWmsDnPostDn zflWmsDnPostDn) {

        if (zflWmsDnPostDn.getVBELN() != null && !"".equals(zflWmsDnPostDn.getVBELN())) {
            zflWmsDnPostDn.setVBELN(String.format("%010d", Long.parseLong(zflWmsDnPostDn.getVBELN())));
        } else {
            zflWmsDnPostDn.setVBELN(String.format("%010d", 0));
        }
        startPage();
        List<ZflWmsDnPostDn> list = zflWmsDnPostDnService.selectZflWmsDnPostDnList1(zflWmsDnPostDn);
        return getDataTable(list);
    }
    /**
     * 质检
     * @param zflWmsDnPostDn1
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    @Log(title = "质检确认", businessType = BusinessType.UPDATE)
    public AjaxResult check(@RequestBody ZflWmsDnPostDn zflWmsDnPostDn1) {
        String prueflos=zflWmsDnPostDn1.getPRUEFLOS();String type = zflWmsDnPostDn1.getTYPE();
        AjaxResult ajaxResult = new AjaxResult();

        ZflWmsDnPostDn zflWmsDnPostDn = zflWmsDnPostDnService.selectZflWmsDnPostDisticnctByprueflos(prueflos);
        if (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getTYPE())) {
            if ("OK".equals(type)) {
                zflWmsDnPostDnService.updateZflWmsDnPostDnZjOk(prueflos);
            } else {
                zflWmsDnPostDnService.updateZflWmsDnPostDnZjNo(prueflos);
            }
            ajaxResult.put("status", "操作成功");
        } else {
            ajaxResult.put("status", "该质检已完成无需重复操作");
        }

        return ajaxResult;

    }

    @RequestMapping("/autoAllocation")
    @ResponseBody
    @Log(title = "出库分配", businessType = BusinessType.INSERT)
    public Map<String, Object> autoAllocation(@RequestBody  TmMatnr tmMatn) {
        String stockin_guid = tmMatn.getInStockId();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        stockin_guid = String.format("%010d",Integer.parseInt(stockin_guid));
        ZflWmsGetDn zflWmsGetDn = ZflWmsGetDnService.selectZflWmsGetDnById1(stockin_guid);
        if (zflWmsGetDn == null) {
            sapController.getDNData(stockin_guid, "", "");
        }
        OutStockout outStockout = outStockoutService.selectOutStockoutById(stockin_guid);
        String stockOutId [] =new String[1];
        if(outStockout !=null){
            stockOutId[0]=outStockout.getStockoutId();
            return outStockoutService.autoAllocation(stockOutId);
        }else{
            resultMap.put("errorInfo", "没有对应交货单");
            return resultMap;
        }

    }

    /**
     * 确认出库
     * @param tmMatn
     * @return
     */
    @RequestMapping("/sureOut")
    @ResponseBody
    @Log(title = "出库确认", businessType = BusinessType.UPDATE)
    public AjaxResult sureOut(@RequestBody  TmMatnr tmMatn) {
        String fbarcode = tmMatn.getFbarcode();
        String stockin_guid = tmMatn.getInStockId();
        AjaxResult ajaxResult = new AjaxResult();
//        ZflWmsGetDn zflWmsGetDn = ZflWmsGetDnService.selectZflWmsGetDnById(stockin_guid);
        stockin_guid = String.format("%010d", Long.parseLong(stockin_guid));
        Surebarcode surebarcode = new Surebarcode();
        surebarcode.setFbarcode(fbarcode);
        surebarcode.setStockoutGuid(stockin_guid);
        List<Surebarcode> surebarcodes = surebarcodeService.selectSurebarcodeList(surebarcode);

        if (surebarcodes.size() > 0) {
            Surebarcode surebarcode1 = surebarcodes.get(0);
            MatnrInfo matnrInfo = null;
            matnrInfo = pdaService.selectMatnrBdById(surebarcode1.getProductDetailId());
            if (matnrInfo == null) {
                List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfo(surebarcode1.getProductDetailId().split("-")[0]);
                if (matnrInfoList.size() > 0) {
                    for (MatnrInfo m : matnrInfoList) {
                        pdaService.insetMatnrInfo(m);
                    }
                }
            }
            matnrInfo = pdaService.selectMatnrBdById(surebarcode1.getProductDetailId());
            if (matnrInfo != null) {
                MatnrInfo matnrInfo1 = pdaService.selectMatnrBdById(surebarcode1.getProductDetailId().split("-")[0]);
                List<TmMatnr> tmMatnrs = pdaService.selectTmMatnrByfbarcode(fbarcode.substring(0, 18), fbarcode.substring(13, 22));
                if (tmMatnrs.size() < matnrInfo.getFnum().intValue() + 1) {
                    TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
                    if (tmMatnr1 == null) {
                        int count = pdaService.selectTmMatnrByFCount(surebarcode1.getProductDetailId(), surebarcode1.getStockoutGuid(), surebarcode1.getProductId());
                        if (count == 0) {

                            TmMatnr tmMatnr = new TmMatnr();
                            tmMatnr.setMatnr(matnrInfo.getMatnr());
                            tmMatnr.setMaktx(matnrInfo.getMaktx());
                            tmMatnr.setMatnrMain(surebarcode1.getProductId());
                            tmMatnr.setYkutxt(matnrInfo.getYkutxt());
                            tmMatnr.setEwbez(matnrInfo.getEwbez());
                            tmMatnr.setFunit(matnrInfo.getFunit());
                            tmMatnr.setFpackunit(matnrInfo.getFpackunit());
                            tmMatnr.setFflag(matnrInfo.getFflag());
                            tmMatnr.setFnum(matnrInfo.getFnum());
                            tmMatnr.setBarcode("");
                            tmMatnr.setFbarcode(fbarcode);
                            tmMatnr.setFid(surebarcode1.getBarcode());
                            tmMatnr.setStatus(-1);
                            tmMatnr.setLotNo(surebarcode1.getLotNo());
                            tmMatnr.setInStockId(surebarcode1.getStockoutGuid());
                            tmMatnr.setInStockDId(surebarcode1.getVGPOS());
                            matnrService.insertTmMatnr(tmMatnr);
                            List<TmMatnr> tmMatnrss = pdaService.selectTmMatnrByfbarcode(fbarcode.substring(0, 18), fbarcode.substring(13, 22));
                            for (TmMatnr tmMatnra : tmMatnrss) {

                                if (tmMatnra != null && tmMatnra.getStatus() == -1 && "1".equals(tmMatnra.getFflag())) {
                                    String productLotNo = "";//是否批次管理
                                    String productXlh = "";//序列号
                                    Product product = productService.selectProductByProductId(String.format("%018d", Long.parseLong(tmMatnra.getMatnr().split("-")[0])));
                                    if (product != null) {
                                        productLotNo = product.getSpecialFlag();
                                        productXlh = product.getProductFlag();
                                    }
                                    ZflWmsGetDn zflWmsGetDn = new ZflWmsGetDn();
                                    zflWmsGetDn.setMATNR(String.format("%018d", Long.parseLong(tmMatnra.getMatnr().split("-")[0])));
                                    zflWmsGetDn.setVBELN(tmMatnra.getInStockId());
                                    List<ZflWmsGetDn> zflWmsGetDns = ZflWmsGetDnService.selectZflWmsGetDnList(zflWmsGetDn);
                                    for (ZflWmsGetDn zflWmsGetDna : zflWmsGetDns) {
                                        String lotNo = "";
                                        ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();

                                        zflWmsDnPostDn.setZEILE(zflWmsGetDna.getPOSNR());
                                        zflWmsDnPostDn.setVBELN(zflWmsGetDna.getVBELN());
                                        zflWmsDnPostDn.setPOSNR(zflWmsGetDna.getPOSNR());
                                        zflWmsDnPostDn.setMATNR(zflWmsGetDna.getMATNR());
                                        zflWmsDnPostDn.setWERKS(zflWmsGetDna.getWERKS());
                                        zflWmsDnPostDn.setLGORT(zflWmsGetDna.getLGORT());
                                        zflWmsDnPostDn.setVRKME(zflWmsGetDna.getVRKME());

                                        if ("X".equals(productLotNo)) {
                                            zflWmsDnPostDn.setCHARG(tmMatnr.getLotNo());
                                            int size = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn).size();

                                            zflWmsDnPostDn.setLFIMG(matnrInfo.getFnum().toString());
                                            zflWmsDnPostDn.setPIKMG(matnrInfo.getFnum().toString());
                                            zflWmsDnPostDn.setStatus("-1");
                                            if (size == 0) {
                                                zflWmsDnPostDnService.insertZflWmsDnPostDn(zflWmsDnPostDn);
                                            } else {
                                                ZflWmsDnPostDn zflWmsDnPostDna1 = new ZflWmsDnPostDn();
                                                zflWmsDnPostDna1.setZEILE(zflWmsGetDna.getPOSNR());
                                                zflWmsDnPostDna1.setVBELN(zflWmsGetDna.getVBELN());
                                                ZflWmsDnPostDn zflWmsDnPostDn1 = zflWmsDnPostDnService.selectZflWmsDnPostDn(zflWmsDnPostDna1);

                                                zflWmsDnPostDna1.setLFIMG((String.valueOf(new BigDecimal(zflWmsDnPostDn1.getLFIMG()).intValue() + 1)));
                                                zflWmsDnPostDna1.setPIKMG((String.valueOf(new BigDecimal(zflWmsDnPostDn1.getPIKMG()).intValue() + 1)));
                                                zflWmsDnPostDnService.updateZflWmsDnPostDn(zflWmsDnPostDna1);
                                            }
                                        }
                                        if ("X".equals(productXlh)) {
                                            zflWmsDnPostDn.setSERNR(tmMatnr.getFbarcode().substring(4, fbarcode.length()));
                                            int size = zflWmsDnPostDnService.selectZflWmsDnPostDnList(zflWmsDnPostDn).size();
                                            zflWmsDnPostDn.setStatus("-1");

                                            zflWmsDnPostDn.setLFIMG(matnrInfo.getFnum().toString());
                                            zflWmsDnPostDn.setPIKMG(matnrInfo.getFnum().toString());
                                            if (size == 0) {
                                                zflWmsDnPostDnService.insertZflWmsDnPostDn(zflWmsDnPostDn);
                                            }
                                        }


                                    }
                                }
                                int i = pdaService.selectCountOut(stockin_guid);
                                ZflWmsDnPostDn zflWmsDnPostDn = new ZflWmsDnPostDn();
                                zflWmsDnPostDn.setVBELN(stockin_guid);
                                zflWmsDnPostDn.setStatus("-1");
                                int size = zflWmsDnPostDnService.selectZflWmsDnPostDnCount(zflWmsDnPostDn);
                                if (size == i) {
                                    zflWmsDnPostDn.setStatus("0");
                                    zflWmsDnPostDnService.updateZflWmsDnPostDnStatus(zflWmsDnPostDn);
                                }

                            }
                            BasStockDetail basStockDetail = basStockDetailService.selectBasStockDetailById(fbarcode);
                            if (basStockDetail != null) {
                                basStockDetailService.insertBasStockDetailfinish(basStockDetail);
                                basStockDetailService.deleteBasStockDetailById(fbarcode);
                            }
                            ajaxResult.put("status", "出货成功");
//                            surebarcode1.getBarcode()
                            outStockoutDetailService.updateOutStockoutDetailBYId(surebarcode1.getBarcode());
                            if (tmMatnrss.size() == matnrInfo.getFnum().intValue()) {
                                TmMatnr tmMatnr11 = new TmMatnr();
                                tmMatnr11.setStatus(0);
                                tmMatnr11.setBarcode(surebarcode.getBarcode());
                                tmMatnr11.setInStockId(surebarcode.getStockoutGuid());
                                matnrService.updateTmMatnr(tmMatnr11);
                            }
                        } else {
                            ajaxResult.put("status", "该单据的物料子件数量已达分配上限");
                        }
                    } else {
                        ajaxResult.put("status", "该条码已扫描，请勿重复扫描");
                    }
                }
            }
        } else {
            ajaxResult.put("status", "或者条码已出库,或扫描条码不在对应托盘");
        }
        pdaService.finishPK();
        return ajaxResult;

    }

    /**
     *
     * @param outTrayMsg
     * @return
     */
    @RequestMapping("/listoutTrayMsg")
    @ResponseBody
    @Log(title = "出库信息查询", businessType = BusinessType.OTHER)
    public TableDataInfo list(@RequestBody OutTrayMsg outTrayMsg)
    {
        if (outTrayMsg.getStockoutGuid() != null && !"".equals(outTrayMsg.getStockoutGuid())) {
            outTrayMsg.setStockoutGuid(String.format("%010d", Long.parseLong(outTrayMsg.getStockoutGuid())));
        } else {
            outTrayMsg.setStockoutGuid(String.format("%010d", 0));
        }
        startPage();
        List<OutTrayMsg> list = outTrayMsgService.selectOutTrayMsgList(outTrayMsg);
        return getDataTable(list);
    }
    @RequestMapping("/listTmMatnr")
    @ResponseBody
    public TableDataInfo list1(@RequestBody TmMatnr tmMatnr)
    {
        startPage();
        tmMatnr.setFid(tmMatnr.getInStockId());
        tmMatnr.setInStockId(tmMatnr.getInStockId().split("-")[0]);

        List<TmMatnr> list = tmMatnrService.selectTmMatnrList(tmMatnr);
        return getDataTable(list);
    }
    /**
     * 配套入库
     *
     * @param basStockDetail
     * @return
     */
    @RequestMapping("/tms/listCg")
    @ResponseBody
    @Log(title = "配套上架信息查询", businessType = BusinessType.OTHER)
    public Map selectTmsMsgCg(@RequestBody BasStockDetail basStockDetail) {
        String fbarcode=basStockDetail.getFbarcode(); String trayCode =basStockDetail.getTrayCode();
        Map map = new HashMap<>();
        Barcode barcode = null;
        Barcode barcode1 = pdaService.selectBarcodeBd(fbarcode);
        if (barcode1 == null) {
            barcode = barcodeService.selectBarcodeByIdHs(fbarcode);
            if (barcode != null) {
                pdaService.saveBarcode(barcode);
            }
        } else {
            barcode = barcode1;
        }
        String stockinGuid = "";
        if (barcode != null) {
            TmMatnr tmMatnr1 = pdaService.selectTmMatnrByFid(fbarcode);
            if (tmMatnr1 != null && tmMatnr1.getStatus() > 1) {
                map.put("status", tmMatnr1.getStatus());
                return map;
            } else if (tmMatnr1 == null) {
                map.put("status", "此条码未收货");
            }
            if (tmMatnr1 != null) {
                ZflWmsDnPostDn zflWmsDnPostDn = zflWmsDnPostDnService.selectZflWmsDnPostDnByPAI(tmMatnr1.getMatnrMain(), tmMatnr1.getInStockId());
                if (zflWmsDnPostDn != null && "X".equals(zflWmsDnPostDn.getTYPE()) && zflWmsDnPostDn.getPRUEFLOS() != null) {
                    map.put("status", "此条码检验不通过，不能入库");
                }
                if (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getTYPE()) && zflWmsDnPostDn.getPRUEFLOS() != null) {
                    map.put("status", "此条码未检验，不能入库");
                }
                if (zflWmsDnPostDn == null|| (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getStatus()))) {
                    map.put("status", "此条码未检验，不能入库");
                }
            }
            //用物料和单据号查询子单据信息，如果有此物料的单据
//            String barcodes = String.format("%018d", Long.parseLong(barcode.getMatnr().split("-")[0]));
            List<MatnrInfo> matnrInfoList = pdaService.selectMatnrInfobd(barcode.getMatnr().split("-")[0]);


            if (matnrInfoList.size() == 0) {
                map.put("status", "此条码母子件信息未维护");
            } else {
                map.put("nowMatnr", barcode.getMatnr());

                map.put("quantity", barcode.getFnum());
            }
            //查询tm_matnr表里是否已有此条
            //从t_barcode_info表中查询fid值


        } else {
            map.put("status", "此条码未收货1");
        }
        List<MydoJobDetail> lists = pdaService.selectJobDet(trayCode);
        if (lists.size() > 0) {
            map.put("matnrList", lists);//成品只能查出一条母件信息
            map.put("qtyout", lists.size());
        } else {
            map.put("qtyout", 0);
        }
        return map;

    }


    @RequestMapping("/saveJob")
    @ResponseBody
    @Log(title = "确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJob(@RequestBody  Inner inner) {
        AjaxResult ajaxResult = pdaService.insetMydoJob(inner.getTrayCode(), inner.getQuantity().intValue(), inner.getFbarcode());
        return ajaxResult;

    }

    @RequestMapping("/sureIn")
    @ResponseBody
    public AjaxResult sureIn(@RequestBody  Inner inner) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("无该托盘任务");
        }
        pdaService.updateJobStatus(trayCode);
        return AjaxResult.success("操作成功");
    }


    @RequestMapping("/saveJobP")
    @ResponseBody
    @Log(title = "确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJobP(@RequestBody Inner inner) {
        AjaxResult ajaxResult = pdaService.insertMydoJobFinish(inner.getCargoSpaceId(), inner.getFbarcode(), inner.getQuantity().intValue(), inner.getTrayCode());
        return ajaxResult;
    }

    @RequestMapping("/sureInP")
    @ResponseBody
    @Log(title = "确认上架完成", businessType = BusinessType.INSERT)
    public AjaxResult sureInP(@RequestBody  Inner inner ) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("未扫描信息");
        }
        pdaService.completeJob(trayCode);
        return AjaxResult.success("入库成功");
    }
    //    @Scheduled(fixedRate = 30 * 1000)
    @RequestMapping("/finish_pk")
    @ResponseBody
    public void finishPK() {

        pdaService.finishPK();
    }
    @Scheduled(cron = "0 0 13 * * ?")
    @RequestMapping("/wms_data_in_del")
    @ResponseBody
    public void wms_data_in_del() {

        pdaService.wms_data_in_del();
    }
    static class Inner {
        private String fbarcode;
        private BigDecimal quantity;
        private String trayCode;
        private String cargoSpaceId;

        public String getCargoSpaceId() {
            return cargoSpaceId;
        }

        public void setCargoSpaceId(String cargoSpaceId) {
            this.cargoSpaceId = cargoSpaceId;
        }

        public String getFbarcode() {
            return fbarcode;
        }

        public void setFbarcode(String fbarcode) {
            this.fbarcode = fbarcode;
        }

        public BigDecimal getQuantity() {
            return quantity;
        }

        public void setQuantity(BigDecimal quantity) {
            this.quantity = quantity;
        }

        public String getTrayCode() {
            return trayCode;
        }

        public void setTrayCode(String trayCode) {
            this.trayCode = trayCode;
        }

        @Override
        public String toString() {
            return "Inner{" +
                    "fbarcode='" + fbarcode + '\'' +
                    ", quantity='" + quantity + '\'' +
                    ", trayCode='" + trayCode + '\'' +
                    '}';
        }
    }
}
